package com.easytouch.vm.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.easytouch.core.MonitorThread;
import com.easytouch.vm.AppApplication;

/**
 * 使用百度SDK处理定位
 * Created by gulin on 2017/8/3.
 */

public class BaiduGPSHandler extends Thread{

    private LocationClient mLocationClient;

    public BaiduGPSHandler()
    {

    }

    private void init(Context context) {
        if(context == null)
        {
            return;
        }
        mLocationClient = new LocationClient(context);
        mLocationClient.registerLocationListener(new MyLocationListener());
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
        option.setCoorType("gcj02");//返回的定位结果是百度经纬度，默认值gcj02
        option.setScanSpan(600000);//设置发起定位请求的间隔时间为10分钟
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    @Override
    public void run()
    {
        super.run();
        init(AppApplication.context);
        try {
            while(true)
            {
                sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 实现实位回调监听
     */
    public static class MyLocationListener implements BDLocationListener
    {
        @Override
        public void onReceiveLocation(BDLocation location)
        {
            //Receive Location
            MonitorThread.setLocationInfo(location.getLongitude(), location.getLatitude(), location.getLocType());
        }
    }
}
